<template>
  <div class="medical-layout">
    <!-- Header -->
    <el-header class="medical-header slide-in">
      <div class="header-left">
        <img
          src="https://picsum.photos/50"
          alt="系统Logo"
          class="logo"
        />
        <h1 class="system-title">溃疡性结肠炎智能评分系统</h1>
      </div>
      <div class="header-right">
        <!-- 🚀 P0网络修复：网络状态指示器 (已禁用) -->
        <!--
        <div class="network-status-indicator scale-in" :class="networkStatusClass">
          <el-tooltip :content="networkStatusText" placement="bottom">
            <div class="network-status-icon">
              <el-icon v-if="networkStatus.isOnline && networkStatus.failedRequests === 0">
                <Connection />
              </el-icon>
              <el-icon v-else-if="networkStatus.isOnline && networkStatus.failedRequests < 3">
                <Warning />
              </el-icon>
              <el-icon v-else>
                <Close />
              </el-icon>
            </div>
          </el-tooltip>
        </div>
        -->

        <!-- 主题切换按钮 -->
        <ThemeToggle class="theme-toggle-component scale-in" />

        <el-dropdown @command="handleCommand">
          <div class="user-info scale-in">
            <el-avatar :size="32" :icon="UserFilled" />
            <span class="username">{{ userName }}</span>
            <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">
                <el-icon><User /></el-icon>
                个人信息
              </el-dropdown-item>
              <el-dropdown-item command="logout" divided>
                <el-icon><SwitchButton /></el-icon>
                退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <el-container class="main-container">
      <!-- 左侧菜单 -->
      <el-aside :width="sidebarWidth" class="medical-sidebar fade-in">
        <el-menu
          :default-active="activeMenu"
          class="medical-menu"
          :collapse="isCollapsed"
          :unique-opened="true"
          router
        >
          <el-menu-item index="/scoring/new">
            <el-icon><Document /></el-icon>
            <template #title>开始评分</template>
          </el-menu-item>

          <el-menu-item index="/scoring/history">
            <el-icon><List /></el-icon>
            <template #title>评分历史</template>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main class="medical-main">
        <div class="content-container">
          <router-view />
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import {
  UserFilled,
  ArrowDown,
  User,
  SwitchButton,
  Document,
  List,
  Connection,
  Warning,
  Close
} from '@element-plus/icons-vue'
import ThemeToggle from '@/components/ThemeToggle.vue'
// import { useNetworkMonitor, getConnectionDescription } from '@/utils/networkMonitor' // 已禁用

const store = useStore()
const route = useRoute()
const router = useRouter()

// 🚀 P0网络修复：集成网络监控 (已禁用)
// const { networkStatus } = useNetworkMonitor()

// 计算属性
const isCollapsed = computed(() => store.state.sidebarCollapsed)
const sidebarWidth = computed(() => isCollapsed.value ? '64px' : '200px')
const userName = computed(() => store.getters['auth/userName'])
const activeMenu = computed(() => route.path)

// 🚀 P0网络修复：网络状态计算属性 (已禁用)
/*
const networkStatusClass = computed(() => {
  if (!networkStatus.isOnline) return 'network-offline'
  if (networkStatus.failedRequests >= 3) return 'network-error'
  if (networkStatus.failedRequests > 0) return 'network-warning'
  return 'network-good'
})

const networkStatusText = computed(() => {
  return getConnectionDescription()
})
*/

// 处理下拉菜单命令
const handleCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人信息功能开发中')
      break
    case 'logout':
      await handleLogout()
      break
  }
}

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要退出登录吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )

    await store.dispatch('auth/logout')
    ElMessage.success('退出登录成功')
    router.push('/login')
  } catch (error) {
    // 用户取消操作
  }
}
</script>

<style scoped>
/* ===== UC Layout System ===== */
.medical-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: var(--uc-bg-primary);
}

/* ===== 毛玻璃头部导航栏 ===== */
.medical-header {
  /* 苹果风格毛玻璃效果 */
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  background-color: rgba(242, 242, 247, 0.8);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);

  /* 浮动设计 - 层次感 */
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--uc-shadow-sm);

  /* 布局 */
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 80px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 深色模式头部 */
@media (prefers-color-scheme: dark) {
  .medical-header {
    background-color: rgba(29, 29, 31, 0.8);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  }
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  object-fit: cover;
  box-shadow: var(--uc-shadow-sm);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.logo:hover {
  transform: scale(1.05);
}

.system-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  color: var(--uc-text-primary);
  letter-spacing: 0.2px;
  transition: color 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* 主题切换组件样式 */
.theme-toggle-component {
  margin-right: 8px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  background-color: rgba(0, 0, 0, 0.02);
  border: 1px solid var(--uc-border-primary);
}

.user-info:hover {
  background-color: var(--uc-accent-primary);
  color: white;
  transform: scale(1.02);
  box-shadow: var(--uc-shadow-sm);
}

.username {
  font-size: 14px;
  font-weight: 500;
  color: inherit;
}

.dropdown-icon {
  font-size: 12px;
  color: inherit;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.user-info:hover .dropdown-icon {
  transform: translateY(1px);
}

/* ===== 主容器 ===== */
.main-container {
  flex: 1;
  height: calc(100vh - 80px);
  background-color: var(--uc-bg-primary);
}

/* ===== 毛玻璃侧边栏 ===== */
.medical-sidebar {
  /* 苹果风格毛玻璃侧边栏 */
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  background-color: rgba(248, 249, 250, 0.6);
  border-right: 1px solid var(--uc-border-primary);

  /* 浮动效果 */
  position: relative;
  z-index: 50;
  box-shadow: var(--uc-shadow-sm);

  /* 平滑过渡 */
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 深色模式侧边栏 */
@media (prefers-color-scheme: dark) {
  .medical-sidebar {
    background-color: rgba(44, 44, 46, 0.6);
    border-right: 1px solid rgba(255, 255, 255, 0.08);
  }
}

.medical-menu {
  border-right: none;
  background-color: transparent;
  padding: 16px 0;
}

/* 菜单项重设计 */
.medical-menu .el-menu-item {
  margin: 8px 12px;
  padding: 12px 16px;
  border-radius: 12px;
  color: var(--uc-text-primary);
  border: 1px solid transparent;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  font-weight: 500;
}

.medical-menu .el-menu-item:hover {
  background-color: var(--uc-accent-primary) !important;
  color: white !important;
  transform: translateX(4px);
  border-color: var(--uc-accent-primary);
  box-shadow: var(--uc-shadow-sm);
}

.medical-menu .el-menu-item.is-active {
  background-color: var(--uc-accent-primary) !important;
  color: white !important;
  border-color: var(--uc-accent-primary);
  box-shadow: var(--uc-shadow-md);
  transform: translateX(4px);
}

.medical-menu .el-menu-item .el-icon {
  margin-right: 12px;
  width: 20px;
  height: 20px;
  font-size: 16px;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.medical-menu .el-menu-item:hover .el-icon {
  transform: scale(1.1);
}

.medical-menu .el-menu-item.is-active .el-icon {
  transform: scale(1.1);
}

/* ===== 主内容区 ===== */
.medical-main {
  background-color: var(--uc-bg-primary);
  padding: 0;
  position: relative;
}

.content-container {
  height: 100%;
  padding: 24px;
  overflow-y: auto;
  max-width: 1400px;
  margin: 0 auto;
}

/* ===== 响应式设计 ===== */
@media (max-width: 1024px) {
  .content-container {
    max-width: 100%;
    padding: 16px;
  }
}

@media (max-width: 768px) {
  .medical-header {
    padding: 0 16px;
    height: 70px;
  }

  .system-title {
    font-size: 18px;
  }

  .username {
    display: none;
  }

  .medical-sidebar {
    position: fixed;
    z-index: 200;
    height: 100vh;
    box-shadow: var(--uc-shadow-lg);
  }

  .medical-main {
    margin-left: 0;
  }

  .content-container {
    padding: 16px;
  }
}

@media (max-width: 480px) {
  .medical-header {
    padding: 0 12px;
    height: 64px;
  }

  .system-title {
    font-size: 16px;
  }

  .logo {
    width: 40px;
    height: 40px;
  }

  .content-container {
    padding: 12px;
  }
}

/* ===== Element Plus 组件深度定制 ===== */

/* 折叠菜单 */
:deep(.el-menu--collapse) {
  width: 80px;
}

:deep(.el-menu--collapse .el-menu-item) {
  margin: 8px 8px;
  padding: 12px;
  justify-content: center;
}

:deep(.el-menu--collapse .el-menu-item .el-icon) {
  margin-right: 0;
}

/* 下拉菜单 */
:deep(.el-dropdown-menu) {
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  background-color: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--uc-border-primary);
  border-radius: 12px;
  box-shadow: var(--uc-shadow-lg);
  padding: 8px;
}

@media (prefers-color-scheme: dark) {
  :deep(.el-dropdown-menu) {
    background-color: rgba(28, 28, 30, 0.9);
    border: 1px solid rgba(255, 255, 255, 0.1);
  }
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-radius: 8px;
  margin: 4px 0;
  color: var(--uc-text-primary);
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

:deep(.el-dropdown-menu__item:hover) {
  background-color: var(--uc-accent-primary);
  color: white;
  transform: translateX(2px);
}

:deep(.el-dropdown-menu__item .el-icon) {
  width: 18px;
  height: 18px;
  font-size: 14px;
}

/* 分割线 */
:deep(.el-dropdown-menu__item.is--divided) {
  border-top: 1px solid var(--uc-border-secondary);
  margin-top: 8px;
  padding-top: 16px;
}

/* 动画增强 */
.medical-header,
.medical-sidebar {
  animation: slideDown 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 🚀 P0网络修复：网络状态指示器样式 */
.network-status-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  margin-right: 12px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}

.network-status-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: var(--uc-text-secondary);
  transition: all 0.3s ease;
}

/* 网络状态颜色 */
.network-good {
  background-color: rgba(52, 199, 89, 0.1);
  border: 1px solid rgba(52, 199, 89, 0.2);
}

.network-good .network-status-icon {
  color: #34c759;
}

.network-warning {
  background-color: rgba(255, 149, 0, 0.1);
  border: 1px solid rgba(255, 149, 0, 0.2);
}

.network-warning .network-status-icon {
  color: #ff9500;
  animation: pulse-warning 2s infinite;
}

.network-error {
  background-color: rgba(255, 59, 48, 0.1);
  border: 1px solid rgba(255, 59, 48, 0.2);
}

.network-error .network-status-icon {
  color: #ff3b30;
  animation: pulse-error 1.5s infinite;
}

.network-offline {
  background-color: rgba(142, 142, 147, 0.1);
  border: 1px solid rgba(142, 142, 147, 0.2);
}

.network-offline .network-status-icon {
  color: #8e8e93;
}

/* 警告脉冲动画 */
@keyframes pulse-warning {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.6;
  }
}

/* 错误脉冲动画 */
@keyframes pulse-error {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.7;
    transform: scale(1.1);
  }
}

/* 悬停效果 */
.network-status-indicator:hover {
  transform: scale(1.05);
  box-shadow: 0 0 0 4px rgba(0, 122, 255, 0.1);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .network-status-indicator {
    width: 28px;
    height: 28px;
    margin-right: 8px;
  }

  .network-status-icon {
    font-size: 14px;
  }
}
</style>